package com.woniuxy.invest.service.impl;

import com.woniuxy.commons.service.FeignProjectService;
import com.woniuxy.commons.util.SnowflakeIdWorker;
import com.woniuxy.commons.entity.Invest;
import com.woniuxy.invest.mapper.InvestMapper;
import com.woniuxy.invest.service.InvestService;
import com.woniuxy.commons.vo.InvestVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class InvestServiceImpl implements InvestService {

    @Resource
    private InvestMapper investMapper;

    @Resource
    private FeignProjectService projectService;

    @Transactional
    @Override
    public boolean addInvest(InvestVo investVo) {
        Invest invest = new Invest();
        invest.setByInvestVo(investVo);
        //  生成项目编号
        SnowflakeIdWorker worker = new SnowflakeIdWorker(1001);
        String no = worker.nextId() + "";
        invest.setIno(no);
        BigDecimal smoney = invest.getLmoney().add(invest.getInterest()).add(invest.getRinterest());
        invest.setSmoney(smoney);
        invest.setLtime(
                LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault())
        );
        projectService.bid(invest);
        
        return investMapper.addInvest(invest);
    }
}
